<template>
  <transition name="slide">
    <div class="mv">
      <div class="back" @click="back">
        <i class="icon-back"></i>
        <span class="title">{{ title }}</span>
      </div>
      <video
        class="mvPlayModel"
        :src="MVsrc"
        :poster="cover_pic"
        controls
        preload
      ></video>
      <!-- 切换栏 -->
      <div class="tab">
        <div
          class="tabItem"
          :class="isactive === true ? 'active' : ''"
          @click="chooseTab"
        >
          相关推荐
        </div>
        <div
          class="tabItem"
          :class="isactive === true ? '' : 'active'"
          @click="chooseTab"
        >
          用户评论
        </div>
      </div>
      <scroll
        :data="mvRecommend"
        ref="scroll"
        class="wrapper"
        v-show="isactive"
        :probeType="probeType"
        :listenScroll="listenScroll"
      >
        <div class="content">
          <van-loading
            class="loading"
            type="spinner"
            v-show="mvRecommend != null && mvRecommend.length === 0"
            >数据加载中...</van-loading
          >
          <div
            v-for="item in mvRecommend"
            :key="item.v_id"
            class="recommendItem"
            @click="selectMv(item)"
          >
            <img class="itemImg" v-lazy="item.cover_pic"/>
            <div class="info">
              <p class="infoName">{{ item.name }}</p>
              <p class="infoCount">播放次数：{{ item.playcnt }}</p>
            </div>
          </div>
        </div>
      </scroll>
      <scroll :data="commentList" v-show="!isactive" ref="scroll"
        class="wrapper" :probeType="probeType"
        :listenScroll="listenScroll">
        <div class="content">
          <van-loading
            class="loading"
            type="spinner"
            v-show="commentList != null && commentList.length === 0"
            >数据加载中...</van-loading
          >
          <div v-for="item in commentList" :key="item.commentid" class="commentItem">
            <img class="userPic" :src="item.avatarurl" :alt="item.avatarurl">
            <div class="userDetail">
              <p class="userName">{{ item.nick }}</p>
              <p class="userComment"> {{ item.rootcommentcontent }}</p>
            </div>
          </div>
        </div>
      </scroll>
      <p class="noResult" v-show="(isactive && mvRecommend == null ) || (commentList == null && !isactive)">
        暂时没有相关内容哦~
      </p>
    </div>
  </transition>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Scroll from '@/components/base/Scroll'
export default {
  components: {
    Scroll
  },
  data () {
    return {
      MVsrc: '',
      cover_pic: '',
      title: '返回',
      probeType: 1,
      listenScroll: true,
      mvRecommend: [], // 相关推荐mv
      isactive: true,
      commentList: [] // 评论列表
    }
  },
  methods: {
    ...mapMutations(['setPlaying']),
    // 返回功能
    back () {
      this.$router.back()
    },
    // 获取MV 全部信息
    async getMVinfo () {
      let {
        data: { data: res }
      } = await this.$http.get('/api/mv?id=' + this.vid)
      this.cover_pic = res.info.cover_pic
      this.mvRecommend = res.recommend
      // console.log('mvRecommend', this.mvRecommend)
    },
    // 获取mv 播放地址
    async getMVplay () {
      const {
        data: { data: res }
      } = await this.$http.get('/api/mv/url?id=' + this.vid)
      this.MVsrc = res[this.vid][0]
    },
    // 切换页面
    chooseTab () {
      this.isactive = !this.isactive
      if (
        this.isactive === true ||
        this.commentList === null ||
        this.commentList.length > 0
      ) {
        return
      }
      this.getComment()
    },
    // 获取评论
    async getComment () {
      const params = {
        id: this.vid,
        // 有默认值
        // pageNo: this.pageNo,
        type: 1,
        // 有默认值
        // pageSize: this.pageSize,
        biztype: 5
      }
      const { data: { data: res } } = await this.$http.get('/api/comment', { params })
      // console.log(res)
      this.total = res.comment.commenttotal
      this.commentList = res.comment.commentlist
    },
    // 切换视频
    selectMv (item) {
      // console.log(item)
      this.$router.replace('/mv/' + item.vid)
    }
  },
  mounted () {
    this.getMVinfo()
    this.getMVplay()
  },
  activated () {
    if (this.playing === false) {
      return
    }
    this.setPlaying(false)
  },
  computed: {
    ...mapState(['playing']),
    vid () {
      return this.$route.params.vid
    },
    playing () {
      return this.$store.state.playing
    }
  },
  watch: {
    vid () {
      this.mvRecommend = []
      this.commentList = []
      // 先清空再请求

      // 解决从MV返回后还要请求一次的问题  减少请求次数
      if (this.$route.params.vid === undefined) {
        return
      }
      this.getMVinfo()
      this.getMVplay()
      this.isactive = true
    }
  }
}
</script>

<style lang="stylus" scoped>
@import "../../common/stylus/variable"
.slide-enter,.slide-leave-to {
  transform: translateX(100%);
}
.slide-enter-active,.slide-leave-active{
  transition: all 0.8s ease;
}
.mv {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  background: $color-background;
  object-fit: fill;
  z-index: 999 !important;
  .back {
    display: flex;
    width: fit-content;
    height: 50px;
    align-items: center;
    padding-left: 10px;
    i {
      font-size: 22px;
      color: $color-theme;
    }
    span {
      font-size: 18px;
      margin-left: 6px;
      font-weight: 700;
      color: $color-theme;
    }
  }
  .mvPlayModel {
    display: block;
    width: 100%;
    height: 214px;
    outline: none;
  }
  .tab {
    display: flex;
    height: 40px;
    padding: 0 20px;
    .tabItem {
      flex: 1;
      text-align: center;
      box-sizing: border-box;
      line-height: 40px;
      &.active {
        color: $color-theme;
        border-bottom: 2px solid $color-theme;
      }
    }
  }
  .wrapper {
    position: fixed;
    top: 310px;
    bottom: 0;
    left: 0;
    right: 0;
    overflow: hidden;
    display:flex;
    justify-content:center;
    align-items: center;
    .loading {
      height: 100%;
    }
    .noComment {
      color: $color-text-l;
    }
    .recommendItem {
      display:flex;
      align-items:center;
      padding: 10px 20px;
      .itemImg {
        display:block;
        width: 100px;
        height: 56px;
        border-radius: 4px;
        margin-right: 10px;
      }
      .info {
        .infoName {
          width: 100%;
          color: $color-text-l;
          text-overflow: ellipsis;
          line-height: 20px;
          overflow:hidden;
        }
        .infoCount {
          margin-top: 4px;
          width: 100%;
          color: $color-text-ll;
          font-size: 12px;
        }
      }
    }
    .commentItem {
      display: flex;
      padding: 10px 20px;
      .userPic{
        display:block;
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      .userDetail {
        margin-left: 20px;
        line-height: 20px;
        .userName{
          font-weight: 700;
          color: $color-text-ll;
        }
        .userComment{
          font-size: 14px;
        }
      }
    }
  }
  .noResult {
    position:absolute;
    top: 70%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
